var selectedLabServiceIdSec2 = 0;
function loadLabService() {
		$.ajax({
			type:		"GET",
			url:		"../LabAdmin/LoadLabService?set_ajax_view",
			dataType:   "json",		
			success: 	function(data) {
			
					if (data.error.code ==  'OK') {
						$('#labTabContent').html(data.result);
					} else {
						displayError(data.error.mess);				
					}

				}

		});
}

function loadLabServicesOfSelectedGroup(id, selectedLabserviceId, mess) {
	$.ajax({
		type:		"POST",
		url:		"../LabAdmin/ListLabServicesOfSelectedGroupAjax?set_ajax_view",
		data :   "labGroupId=" + id,
		dataType:   "json",		
		success: 	function(data) {
		
				if (data.error.code ==  'OK') {
					var optionts = buildLabServicesSelectOption(data.result, id);
					$("select#labServiceSelect").html(optionts);
					if (selectedLabserviceId != null) {						
						setSelectedLabServiceSec2(selectedLabserviceId);
					}
					showMessage('statusMessage', mess, 'success');
				} else {
					displayError(data.error.mess);				
				}

			}

	});
	
}
function buildLabServicesSelectOption(arrLabServices, labGroupId) {
	var option = "";
	for(var index in arrLabServices){
			option += "<option value='" + arrLabServices[index].service_id 
			+  "'  price='" + arrLabServices[index].price 
			+  "' disabledService='" +  arrLabServices[index].disabled
			+ "'>" 
			+ arrLabServices[index].name;
			if(arrLabServices[index].disabled == 1){
				option += "*";
			}
			option += "</option>";
	}
	return option;
}

function updateLabServiceSec2() {
	if (!validateLabService()) {
		return;
	}	
	var lcInt = locale(decimalPoint, thousandSep, 0);
	var name = $('#labServiceName').val();
	var serviceId = $('#labServiceId').val();
	var price = parseLcNum($('#labServicePrice').val(), lcInt);
	var disabled = $("input:radio[name='serviceDisabled']:checked").val();
	var labGroupId = $('#labGroupSelect :selected').val();	
	$.ajax({
		type:		"POST",
		url:		"../LabAdmin/UpdateLabServiceAjax?set_ajax_view",
		data :   "serviceId=" + serviceId
				+ "&serviceName=" + name
				+ "&price=" + price
				+ "&disabled=" + disabled,
		dataType:   "json",		
		success: 	function(data) {
		
				if (data.error.code ==  'OK') {
					loadLabServicesOfSelectedGroup(labGroupId, serviceId,_('Save successful'));					
				} else {
					displayError(data.error.mess);				
				}

			}

	});
	
}

function addLabServiceSec2() {
	if (!validateLabService()) {
		return;
	}	
	var lcInt = locale(decimalPoint, thousandSep, 0);
	var name = $('#labServiceName').val();
	var price = parseLcNum($('#labServicePrice').val(), lcInt);
	var disabled = $("input:radio[name='serviceDisabled']:checked").val();
	var labGroupId = $('#labGroupSelect :selected').val();	
	$.ajax({
		type:		"POST",
		url:		"../LabAdmin/AddLabServiceAjax?set_ajax_view",
		data :   "labGroupId=" + labGroupId
				+ "&serviceName=" + name
				+ "&price=" + price
				+ "&disabled=" + disabled,
		dataType:   "json",		
		success: 	function(data) {
		
				if (data.error.code ==  'OK') {
					loadLabServicesOfSelectedGroup(labGroupId, data.result,_('Save successful'));					
				} else {
					displayError(data.error.mess);				
				}

			}

	});
	
}

function validateLabService() {
	var lcInt = locale(decimalPoint, thousandSep, 0);
	var name = $('#labServiceName').val();
	if (name == null && $.trim(name) == '') {
		showMessage('statusMessage', _('Laboratory service name is empty'), 'error');
		return false;
	}
	
	var price = parseLcNum($('#labServicePrice').val(), lcInt);
	if (isNaN(price)) {
		showMessage('statusMessage', _('Invalid service price'), 'error');
		return false;		
	}	
	
	showMessage('statusMessage', '', 'success');
	return true;
}



function setSelectedLabServiceSec2(id) {
	var lcInt = locale(decimalPoint, thousandSep, 0);
	$('#labServiceSelect :option').each(function() {
		if ($(this).val() == id) {
				$(this).attr('selected', 'selected');
				$('#labServiceId').val($(this).val());
				$('#labServicePrice').val(toLcString($(this).attr('price'), lcInt));
				var disabled = $(this).attr('disabledService');
				if (disabled == 0) {
					$('#labServiceName').val($(this).html());
					$('#labServiceLocked').attr('checked',false);
					$('#labServiceUnLock').attr('checked',true);
				} else {
					var serviceName = $(this).html();
					$('#labServiceName').val(serviceName.substring(0, serviceName.length -1));
					$('#labServiceLocked').attr('checked',true);
					$('#labServiceUnLock').attr('checked',false);					
				}
			if ($('#labServiceId').val() != '') {
	            $('#labServiceUpdate').attr('disabled',false);
	        } else {
	        	$('#labServiceUpdate').attr('disabled','disabled');
	        }
		} else {
			$(this).attr('selected', false);
		}
		
	});
	
}

function resetLabServiceData() {
	$('#labServiceName').val('');
	$('#labServiceId').val('');
	$('#labServicePrice').val('');
	$('#labServiceLocked').attr('checked',false);
	$('#labServiceUnLock').attr('checked',false);
	$('#labServiceUpdate').attr('disabled',true);
	
}